<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <input type="text" id="message">
    <button id="submit">提交</button>

    <script>
        //创建websocket实例
        let websocket= new WebSocket("ws://127.0.0.1:8080/test");

        // 给实例挂载一些回调函数
        websocket.onopen = function(){
            console.log("连接成功");
        }
        websocket.onmessage = function(e){
            console.log("收到消息: " + e.data);
        }
        websocket.onerror = function(){
            console.log("连接错误");
        }
        websocket.onclose = function(){
            console.log("连接关闭");
        }

        let input = document.querySelector("#message");
        let button = document.querySelector("#submit");
        button.onclick = function(){
            console.log("提交消息" + input.value);
            websocket.send(input.value);
        }
        
    </script>
</body>
</html>